package com.ssm.service.system.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ssm.common.Message;
import com.ssm.common.db.IdPrefix;
import com.ssm.common.db.SequenceId;
import com.ssm.dao.system.RoleDao;
import com.ssm.entity.Ztree;
import com.ssm.entity.system.Role;
import com.ssm.service.BaseService;
import com.ssm.service.system.RoleService;
import com.ssm.web.Page;

@Service
public class RoleServiceImpl extends BaseService implements RoleService {
	
	@Autowired
	private RoleDao roleDao;

	@Override
	public Page<Role> queryList(Role role) {
		Page<Role> page = new Page<>();
		try {
			page.setCount(roleDao.queryCount(role));
			page.setRecord(roleDao.queryList(role));
			page.setCurrentPage(role.getCurrentPage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	@Override
	public Role getDetail(String roleId) {
		return roleDao.getDetail(roleId);
	}
	
	@Override
	@Transactional
	public Message save(Role role) {
		Message message = new Message();
		try {
			role.setId(getId(SequenceId.ROLE, IdPrefix.ROLE, 8));
			role.setCreater("admin");
			roleDao.save(role);
			
			Role rolePrivilege = new Role();
			rolePrivilege.setId(role.getId());
			
			String privilegeId = role.getPrivilegeId();
			if (StringUtils.isNotBlank(privilegeId)) {
				String[] ids = privilegeId.split(",");
				for (String id : ids) {
					rolePrivilege.setPrivilegeId(id);
					roleDao.saveRolePrivilege(rolePrivilege);
				}
			}
			message.setSuccess(true);
		} catch (DataAccessException e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("操作失败");
		}
		return message;
	}
	
	@Override
	public Message delete(String[] ids) {
		Message message = new Message();
		try {
			roleDao.delete(ids);
			for (String id : ids) {
				roleDao.deleteRolePrivilege(id);
			}
		} catch (DataAccessException e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("操作失败");
		}
		return message;
	}
	
	@Override
	public Message update(Role role) {
		Message message = new Message();
		try {
			roleDao.update(role);
			roleDao.deleteRolePrivilege(role.getId());
			
			Role rolePrivilege = new Role();
			rolePrivilege.setId(role.getId());
			
			String privilegeId = role.getPrivilegeId();
			if (StringUtils.isNotBlank(privilegeId)) {
				String[] ids = privilegeId.split(",");
				for (String id : ids) {
					rolePrivilege.setPrivilegeId(id);
					roleDao.saveRolePrivilege(rolePrivilege);
				}
			}
			message.setSuccess(true);
		} catch (DataAccessException e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("操作失败");
		}
		return message;
	}

	@Override
	public List<Ztree> ztree(String roleId) {
		roleId = roleId == null ? "" : roleId;
		List<Ztree> list = roleDao.ztree(roleId);
		
		Ztree ztree = new Ztree();
		ztree.setId("P0000000");
		ztree.setName("根菜单");
		
		boolean flag = false;
		if (StringUtils.isNotBlank(roleId)) {
			for (Ztree z : list) {
				if (z.getFlag() != null) {
					z.setChecked(true);
					flag = true;
				}
			}
		}
		if (flag) {
			ztree.setChecked(true);
		}
		list.add(0, ztree);
		return list;
	}
}
